package com.uenpay.oem.ui.weipay;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.rebound.SimpleSpringListener;
import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringChain;
import com.facebook.rebound.SpringSystem;
import com.uenpay.im.utilities.Toasts;
import com.uenpay.im.zxing.CaptureActivity;
import com.uenpay.oem.R;
import com.uenpay.oem.data.bean.UserAuthInfo;
import com.uenpay.oem.ui.bankcard.MyBankCardActivity;
import com.uenpay.oem.ui.reserver.RechargeActivity;
import com.uenpay.oem.ui.trans.receivable.QRCodeCollectionActivity;
import com.uenpay.util.app.base.BaseActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: liulou
 * Date: 2017/6/14.
 * desc:
 */

public class WeiPayActivity extends BaseActivity implements WeiPayContract.View {

    @BindView(R.id.ll_weipay_horizontal)
    LinearLayout llHorizontalLayout;
    @BindView(R.id.ll_scancode)
    LinearLayout llScanCode;
    @BindView(R.id.ll_paycode)
    LinearLayout llPayCode;
    @BindView(R.id.ll_transfer)
    LinearLayout llTransfer;
    @BindView(R.id.ll_charge)
    LinearLayout llCharge;

    @BindView(R.id.cv_bankcard)
    CardView cvBankCard;
    @BindView(R.id.cv_coupon)
    CardView cvCoupon;
    @BindView(R.id.tv_day)
    TextView tvDay;
    @BindView(R.id.tv_week)
    TextView tvWeek;
    @BindView(R.id.tv_year)
    TextView tvYear;
    @BindView(R.id.ib_close)
    ImageButton ibClose;


    private View[] views;
    private UserAuthInfo mUserAuthInfo = null;
    private String mBalance;
    private WeiPayPresenter Mweipay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_weipay;
    }

    @OnClick({R.id.ll_scancode, R.id.ll_charge, R.id.ll_paycode, R.id.ll_transfer})
    public void scanCodeClick(final View view) {
        viewClickAction(view);
    }

    @Override
    public void initView(View view) {
        setPresenter(null);
        Mweipay.getBalance();
        Mweipay.getUserInfo();

        views = new View[]{llScanCode, llPayCode, llCharge, llTransfer};

        SpringChain springChain = SpringChain.create(40, 6, 50, 7);
        int childCount = llHorizontalLayout.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = llHorizontalLayout.getChildAt(i);
            springChain.addSpring(new SimpleSpringListener() {
                @Override
                public void onSpringUpdate(Spring spring) {
                    child.setTranslationY((float) spring.getCurrentValue());
                }

            });
        }

        List<Spring> springs = springChain.getAllSprings();
        for (int i = 0; i < springs.size(); i++) {
            springs.get(i).setCurrentValue(1000);
        }

        springChain.setControlSpringIndex(0).getControlSpring().setEndValue(0);

        animationTranslationX(cvBankCard, -1000, 0, 500);
        animationTranslationX(cvCoupon, 1000, 0, 500);
        initDate();
    }

    private void initDate() {
        long time = System.currentTimeMillis();
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat("dd");
        tvDay.setText("" + format.format(date));

        format = new SimpleDateFormat("MM/yyyy");
        tvYear.setText("" + format.format(date));

        format = new SimpleDateFormat("EEEE");
        tvWeek.setText("" + format.format(date));

    }

    /**
     * 圆形图标点击效果动画
     *
     * @param views
     */
    private void initCircleBtnClickAnim(final View[] views) {
        SpringSystem springSystem = SpringSystem.create();
        for (final View view : views) {
            Spring spring = springSystem.createSpring();
            spring.addListener(new SimpleSpringListener() {

                @Override
                public void onSpringUpdate(Spring spring) {
                    float scale = (float) spring.getCurrentValue();
                    view.setScaleX(scale);
                    view.setScaleY(scale);

                }
            });
            view.setTag(spring);
        }
    }

    public void animationTranslationX(final View view, float start, float end, long duration) {
        ValueAnimator animator = ValueAnimator.ofFloat(start, end);
        animator.setDuration(duration).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                view.setTranslationX((Float) animation.getAnimatedValue());
            }
        });
    }

    public void animationAlpha(float start, float end, long duration, final View... views) {
        ValueAnimator animator = ValueAnimator.ofFloat(start, end);
        animator.setDuration(duration).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                for (View view : views) {
                    view.setAlpha((Float) animation.getAnimatedValue());
                }
            }
        });
    }

    @OnClick(R.id.ib_close)
    public void close() {
        animationAlpha(1, 0, 400, llHorizontalLayout.getChildAt(0), llHorizontalLayout.getChildAt(1), llHorizontalLayout.getChildAt(2), llHorizontalLayout.getChildAt(3));
        animationTranslationX(cvBankCard, 0, 1000, 200);
        animationTranslationX(cvCoupon, 0, -1000, 200);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                onBackPressed();
            }
        }, 200);
    }

    @Override
    public void back() {
        close();
    }

    @OnClick({R.id.cv_bankcard, R.id.cv_coupon})
    public void cvClick(View view) {
        viewClickAction(view);
    }

    private void viewClickAction(View view) {
        switch (view.getId()) {
            case R.id.ll_scancode:
                Intent intent = new Intent(WeiPayActivity.this, CaptureActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_paycode:
                Intent payCode = new Intent(WeiPayActivity.this, QRCodeCollectionActivity.class);
                startActivity(payCode);
                break;
            case R.id.ll_transfer:

                break;
            case R.id.ll_charge:
                jumpToCharge();
                break;
            case R.id.cv_bankcard:
                Intent intent_bank = new Intent(WeiPayActivity.this, MyBankCardActivity.class);
                startActivity(intent_bank);
                break;
            case R.id.cv_coupon:
                break;
        }
        finish();
    }

    @Override
    public void showUserInfo(UserAuthInfo info) {
        mUserAuthInfo = info;
    }

    @Override
    public void showBalance(String balance) {
        mBalance = balance;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public void setPresenter(Object presenter) {
        Mweipay = new WeiPayPresenter(this);
    }

    @Override
    public void showToast(String msg) {

    }

    /**
     * 跳转到充值页面
     */
    private void jumpToCharge() {

        if (mUserAuthInfo == null) {
            Toasts.shortToast(this, "未获取到用户信息");
            return;
        }
        if (mUserAuthInfo.getNumBC() <= 0) {
            Toasts.shortToast(this, "未绑定银行卡,请先绑定银行卡");
            return;
        }

        Intent recharge = new Intent(this, RechargeActivity.class);
        recharge.putExtra("balance", mBalance);
        startActivity(recharge);
    }
}
